######################
#  Replication code for 'Mediating the Electoral Connection', forthcoming in the JOP
#  John Henderson and John Brooks
#  12/7/2015    
######################    

# figureIII-IV.R
#  :: produces descriptive plots for figure III and figure IV

rm(list=ls())
setwd('~/Dropbox/rainReplication')
      
fes.type=3
non.missings=4
 
source('prelimMain.R') 
          
# day rain hist
pdf('descriptives/rain_day_hist.pdf')                        
hist(rain_data$rain_day[full],xlim=c(-1,2),breaks=60,main='',xlab='(Normalized) Election Rain')
dev.off() 

# weekend rain hist
pdf('descriptives/rain_weekend_hist.pdf')                        
hist(rain_data$rain_weekend[full],xlim=c(-1,2),breaks=80,main='',xlab='(Normalized) Election Rain')
dev.off()  

# ghk rain hist
pdf('descriptives/rain_ghk_hist.pdf')                        
hist(rain_data$dis_rain[full],xlim=c(-1,2),breaks=40,main='',xlab='(Normalized) Election Rain')   
#hist(rain_data$RD_ghk_rain_dev[full],xlim=c(-1,2),breaks=40,main='',xlab='(Normalized) Election Rain')    
dev.off()   

# correlation w/ ghk and analogous rain measure 
# :: normalized
cor.test(rain_data$dis_rain[full],rain_data$rain_elec[full])   

pdf('descriptives/ghk_scatter_1.pdf')
plot(rain_data$dis_rain[full]~rain_data$rain_elec[full],xlab='(Normalized) Election Rain',ylab='Election Rain (GHK)',xlim=c(-1,6))
y=rain_data$dis_rain[full]
x=rain_data$rain_elec[full]       
ix=which(!is.na(x) & !is.na(y))
lines(lowess(y=y[ix],x=x[ix]),col='red',lty=2)   
text(x=-0,y=1.5,label=expression(rho == 0.841),cex=1.5)                                             
dev.off()     

# :: unnormalized
cor.test(rain_data$dis_rain[full],rain_data$rain_elec00[full])       

pdf('descriptives/ghk_scatter_2.pdf')
plot(rain_data$dis_rain[full]~rain_data$rain_elec00[full],xlim=c(-.5,2),xlab='(Unnormalized) Election Rain',ylab='Election Rain (GHK)') 
y=rain_data$dis_rain[full]
x=rain_data$rain_elec00[full]       
ix=which(!is.na(x) & !is.na(y))
lines(lowess(y=y[ix],x=x[ix]),col='red',lty=2)      
text(x=-0,y=1.5,label=expression(rho == 0.901),cex=1.5)                                       
dev.off()                                                             
    
# :: rain_day
cor.test(rain_data$dis_rain[full],rain_data$rain_day[full])       

pdf('descriptives/ghk_scatter_3.pdf')
plot(rain_data$dis_rain[full]~rain_data$rain_day[full],xlim=c(-1,3),xlab=expression('(IV) Election Day Rain'['t']),ylab='Election Rain (GHK)') 
y=rain_data$dis_rain[full]
x=rain_data$rain_day[full]
ix=which(!is.na(x) & !is.na(y))
lines(lowess(y=y[ix],x=x[ix]),col='red',lty=2)   
text(x=-.25,y=1.5,label=expression(rho == 0.497),cex=1.5)                                          
dev.off()                                                             

# END      